﻿package com.nicolasprof.profgallery.thumbs {
	import gs.TweenLite;
	import gs.easing.Sine;

	import com.nicolasprof.profgallery.Engine;
	import com.nicolasprof.profgallery.assets.ThumbnailBG;
	import com.nicolasprof.profgallery.assets.ThumbnailOverFrame;
	import com.nicolasprof.profgallery.viewer.Viewer;

	import flash.display.*;
	import flash.events.*;
	import flash.filters.BlurFilter;
	import flash.filters.DropShadowFilter;

	public class Thumbnail extends Sprite {

		private var _width:Number;
		private var _height:Number;
		private var _data:XML;
		private var _overFrame:ThumbnailOverFrame;
		private var _visited : Sprite;
		private var _viewer : Viewer;
		private var _bg : ThumbnailBG;
		private var _pos : int;
		
		
		
		public function Thumbnail() {
		}	
		public function initiate($width:Number, $height:Number, $data:XML) : void {
			_width = $width;
			_height = $height;
			_data = $data;
			_bg = new ThumbnailBG();	
			_viewer = new Viewer();

			
			//LISTENERS
			this.addEventListener(MouseEvent.MOUSE_OVER, onOver, false, 0, true);
			this.addEventListener(MouseEvent.MOUSE_OUT, onOut, false, 0, true);
			this.addEventListener(MouseEvent.CLICK, onClick, false, 0, true);
			_viewer.addEventListener(Event.COMPLETE, onComplete, false, 0, true);

			//DISPLAY LIST
			addChild(_bg);
			addChild(_viewer);

			//ACTIONS
			_viewer.initiate(_width, _height);
			
			this.buttonMode=true;
			this.tabEnabled = false;
			this.mouseChildren = false;
		}			
		public function draw() : void {
			drawBG();
			drawVisitedIndicator();
			drawOverFrame();	
			initAnimation();
		}
		protected function drawBG() : void {
			var config : XML = Engine.CONFIG_XML;
			var border : int = int(config.thumbnail.@border);
			_bg.width = _width + 2 * border;
			_bg.height = _height + 2 * border;
			_bg.x = _bg.y = -border;
			_bg.mouseEnabled = false;
		}
		protected function drawOverFrame() : void {
			_overFrame=new ThumbnailOverFrame();
			this.addChild(_overFrame);
			_overFrame.mouseEnabled = false;
			_overFrame.width = _width;
			_overFrame.height = _height;
		}
		protected function drawVisitedIndicator() : void {
			_visited = new Sprite();
			
			var config : XML = Engine.CONFIG_XML;
			var visitedTall : int = uint(config.visited.@gabarit);
			var visitedPadding : int = uint(config.visited.@padding);
			
			_visited.mouseEnabled = false;
			_visited.graphics.beginFill(config.visited.@mainColor,config.visited.@alpha);
			_visited.graphics.moveTo(_width - visitedPadding, _height - visitedPadding);
			_visited.graphics.lineTo(_width - (visitedTall + visitedPadding), _height - visitedPadding);
			_visited.graphics.lineTo(_width - visitedPadding, _height - (visitedTall + visitedPadding));
			_visited.graphics.lineTo(_width - visitedPadding, _height - visitedPadding);
			_visited.graphics.endFill();
			_visited.filters = [new DropShadowFilter(0,45,0,1,4,4,0.6)];
			
			addChild(_visited);
		}
		
		protected function initAnimation() : void {
			_bg.alpha = 0;
			_bg.visible = false;
			_overFrame.alpha= 0;
			_overFrame.visible = false;
			_visited.alpha= 0;
			_visited.visible = false;
			_viewer.filters = [new BlurFilter(10,10,1)];
			_viewer.alpha = 0;
		}

		public function loadPhoto() : void {
			_viewer.loadImage(this.thumbURL);
		}

		/**
		 * EVENTS
		 */
		private function onClick(e : MouseEvent) : void {
			TweenLite.to(_visited, 0.5, {autoAlpha:1});
		}
		private function onOver(e:MouseEvent):void {
			TweenLite.to(_overFrame, 0.5, {autoAlpha:1, ease: Sine.easeInOut});
		}
		private function onOut(e:MouseEvent):void {
			TweenLite.to(_overFrame, 0.5, {autoAlpha:0, ease: Sine.easeInOut});
		}
		protected function onComplete(e:Event):void {
			TweenLite.to(_bg, 0.5, {autoAlpha:1});
			TweenLite.to(_viewer, 0.5, {blurFilter:{blurX:0, blurY:0, quality:1}, alpha:1, delay:0.5});
		}
		
		//GETTERS AND SETTERS

		public function get mainURL():String {
			return String(_data.@main);
		}
		public function get pos():int {
			return _pos;
		}
		public function set pos(value : int):void {
			_pos = value;
		}
		public function get thumbURL():String {
			return String(_data.@thumb);
		}
		public function get desc():String {
			return String(_data.desc);
		}
		public function get title():String {
			return String(_data.title);
		}
		public function get url():String {
			return String(_data.@url);
		}
	}
}